import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification, make_moons, make_circles


def test1():
    # 计算x,y坐标对应的高度值
    def f(x, y):
        return (1 - x / 2 + x ** 3 + y ** 5) * np.exp(-x ** 2 - y ** 2)


    # 生成x,y的数据
    n = 256
    x = np.linspace(-3, 3, n)
    y = np.linspace(-3, 3, n)

    # 把x,y数据生成mesh网格状的数据,因为等高线的显示是在网格的基础上添加上高度值
    X, Y = np.meshgrid(x, y)

    # 填充等高线
    plt.contourf(X, Y, f(X, Y))
    # 显示图表
    plt.show()

def test2():
    # X, y = make_classification(n_features=2, n_redundant=0, n_informative=2,
    #                            random_state=1, n_clusters_per_class=1)
    # l = len(y)
    def f(x, y):
        return (1 - x / 2 + x ** 3 + y ** 5) * np.exp(-x ** 2 - y ** 2)
    #
    # plt.contourf(X[:,0], X[:,1],  f(X[:,0], X[:,1]))

    X = [1,2,3,4]
    Y = [1, 2, 3, 4]
    Z = [[1, 0, 1, 0],[0, 0, 1, 0],[1, 1, 1, 0],[1, 0, 0, 0]]


    # xx,yy = np.meshgrid(X,Y)
    # plt.contourf(X, Y, f(xx,yy))
    plt.subplot(2,1,1)
    x,y = make_moons(noise=0.2)
    s= plt.scatter(x[:, 0], x[:, 1], marker='o', c=y)

    plt.subplot(2, 1, 2)
    xx,yy = make_circles(noise=0.2)
    s= plt.scatter(xx[:, 0], xx[:, 1], marker='o', c=yy)
    # 显示图表
    plt.show()
    import sklearn.neighbors as sk_neighbors
    import pickle
    # KNN分类
    model = sk_neighbors.KNeighborsClassifier(n_neighbors=5, n_jobs=1)
    model.fit(xx, yy)
    acc = model.score(xx, yy)  # 根据给定数据与标签返回正确率的均值
    print('KNN模型(分类)评价:', acc)

    # KNN回归
    model = sk_neighbors.KNeighborsRegressor(n_neighbors=5, n_jobs=1)
    model.fit(xx, yy)
    acc = model.score(xx, yy)  # 返回预测的确定系数R2
    print('KNN模型(回归)评价:', acc)


x=np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(x)
print('------------')
print(x[:,::-1])
print('------------')
print(x[:,::1])
print('------------')
print(x[:,::2])
print('------------')
print(x[:,::3])
print('------------')
print(x[:,::666666])

print('------------')
print(x[1:])
print('------------')
print(x[2:])
print('------------')
print(x[:1])
print('------------')
print(x[:2])
print('------------')
print(x[1:2])
print('------------')
print(x[1:2,0])
print('------------')
print(x[1:3,1])
print('------------')
print(x[2:3,1])
print('------------')
print(np.reshape(x[:,1]))
print('------------')
print(x[:,2])
print('------------')
print(x[1:,2])

print('------------')
print(x[1:,1:1])

test2()